﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    widget.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-02-20  16:49
*/
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QDebug>

#include "networkstationident.h"
#include "savestationdialog.h"
#include "testdialog.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    model = new QStandardItemModel();
    model->setColumnCount(5); // 根据NetWorkStation结构体字段数量设置列数
    model->setHorizontalHeaderLabels({"站台类型", "频率", "调制速率", "调制方式", "编码方式"});
    // 将模型设置给tableView
    ui->tableView->setModel(model);
    // 设置表格视图为只读（不可编辑）
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // 设置选择模式为单行选中（可根据需求调整）
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    // 设置最后一列自动填充剩余空间
    ui->tableView->horizontalHeader()->setStretchLastSection(true);
}

Widget::~Widget()
{
    delete model;
    delete ui;
}

void Widget::addStation()
{
    NetWorkStation station;
    station.setSation("网台1");
    station.freq = 100;
    station.modRate = 1000;
    station.setModType("QPSK");
    station.setCodeType("VIT");
    SaveStationDialog *dialog = new SaveStationDialog(&station,"添加",this);
    if(dialog->exec() == QDialog::Accepted){
        int row = model->rowCount();
        model->setItem(row, 0, new QStandardItem(QString(station.station)));
        model->setItem(row, 1, new QStandardItem(QString::number(station.freq)));
        model->setItem(row, 2, new QStandardItem(QString::number(station.modRate)));
        model->setItem(row, 3, new QStandardItem(QString(station.modType)));
        model->setItem(row, 4, new QStandardItem(QString(station.codeType)));
    }
}

void Widget::upStation()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int row = index.row();
    NetWorkStation station;
    station.setSation(model->data(model->index(row, 0)).toString().toStdString());
    station.freq = model->data(model->index(row, 1)).toDouble();
    station.modRate = model->data(model->index(row, 2)).toDouble();
    station.setModType(model->data(model->index(row, 3)).toString().toStdString());
    station.setCodeType(model->data(model->index(row, 4)).toString().toStdString());

    SaveStationDialog *dialog = new SaveStationDialog(&station,"修改",this);
    if(dialog->exec() == QDialog::Accepted){
        model->setItem(row, 0, new QStandardItem(QString(station.station)));
        model->setItem(row, 1, new QStandardItem(QString::number(station.freq)));
        model->setItem(row, 2, new QStandardItem(QString::number(station.modRate)));
        model->setItem(row, 3, new QStandardItem(QString(station.modType)));
        model->setItem(row, 4, new QStandardItem(QString(station.codeType)));
    }
}

void Widget::delStation()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();
    model->removeRow(iSel);
}

void Widget::on_selFile_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,"选择底数表文件","","*.lic");
    if(!path.isEmpty()){
        ui->filePath->setText(path);
        //读取数据到列表内
        NetWorkStationIdent netWorkStationIdent(path.toStdString());
        QVector<NetWorkStation> netWorkStations;

        model->removeRows(0,model->rowCount());
        // 填充数据

        for (const auto& station : netWorkStationIdent.getStations()) {
            int row = model->rowCount();
            model->insertRow(row);
            model->setItem(row, 0, new QStandardItem(QString(station.station)));
            model->setItem(row, 1, new QStandardItem(QString::number(station.freq)));
            model->setItem(row, 2, new QStandardItem(QString::number(station.modRate)));
            model->setItem(row, 3, new QStandardItem(QString(station.modType)));
            model->setItem(row, 4, new QStandardItem(QString(station.codeType)));
        }
    }
}


void Widget::on_saveBtn_clicked()
{
    if(!ui->filePath->text().isEmpty()){
        NetWorkStationIdent netWorkStationIdent(ui->filePath->text().toStdString());
        // 遍历模型中的所有行
        vector<NetWorkStation> stations;
        for (int row = 0; row < model->rowCount(); ++row) {
            NetWorkStation station;

            // 读取每个字段的值
            station.setSation(model->data(model->index(row, 0)).toString().toStdString());
            station.freq = model->data(model->index(row, 1)).toDouble();
            station.modRate = model->data(model->index(row, 2)).toDouble();
            station.setModType(model->data(model->index(row, 3)).toString().toStdString());
            station.setCodeType(model->data(model->index(row, 4)).toString().toStdString());

            // 将NetWorkStation添加到vector中
            stations.push_back(station);
        }
        // 保存到文件
        netWorkStationIdent.setStations(stations);
        if (!netWorkStationIdent.saveToFile(ui->filePath->text().toStdString())) {
            QMessageBox::critical(this,"警告","保存文件失败");
        } else {
            QMessageBox::information(this,"提示","文件保存成功");
        }
    }
}

void Widget::on_tableView_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu(this);
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();
    menu.addAction(QString("添加"),this,&Widget::addStation);
    if(iSel > -1){
        menu.addAction(QString("修改"),this,&Widget::upStation);
        menu.addAction(QString("删除"),this,&Widget::delStation);
    }
    menu.exec(QCursor::pos());
}

void Widget::on_testBtn_clicked()
{
    TestDialog *dialog = new TestDialog(ui->filePath->text());
    dialog->exec();
}
